package com.github.xiaolyuh.enums;


import com.github.xiaolyuh.LocalCacheConfigManager;
import com.github.xiaolyuh.constant.CacheConstant;
import com.github.xiaolyuh.utils.FastJSONHelper;

import java.util.List;

public enum MqMessageTypeEnum {
    ACTIVITY_BASE(1, CacheConstant.DEFAULT_CHANNEL, content -> {
        LocalCacheConfigManager.getInstance().invalidate(LocalCacheInstanceEnum.ACTIVITY_BASE_AND_PRIORITY, FastJSONHelper.deserialize(content, String.class));
    }, "刷新活动基本信息类消息"),
    GOOD_ACTIVITY(2, CacheConstant.DEFAULT_CHANNEL, content -> {
        LocalCacheConfigManager.getInstance().invalidateAll(LocalCacheInstanceEnum.ACTIVITY_INDEX, FastJSONHelper.deserialize(content, List.class));
    }, "刷新商品活动索引缓存类信息"),
    SHOP_ACTIVITY(3, CacheConstant.DEFAULT_CHANNEL, content -> {
        LocalCacheConfigManager.getInstance().invalidateAll(LocalCacheInstanceEnum.ACTIVITY_INDEX, FastJSONHelper.deserialize(content, List.class));
    }, "刷新店铺活动索引缓存类信息"),
    ACTIVITY_PRIORITY(4, CacheConstant.DEFAULT_CHANNEL, content -> {
        LocalCacheConfigManager.getInstance().invalidate(LocalCacheInstanceEnum.ACTIVITY_BASE_AND_PRIORITY, FastJSONHelper.deserialize(content, String.class));
    }, "刷新活动优先级缓存类信息"),;
    /**
     * 消息业务类型
     */
    private Integer type;

    /**
     * 消息类型对于目标地址
     */
    private String destination;

    /**
     * 消息对应的处理器
     */
    private MqMessageProcessor processor;

    private String desc;

    MqMessageTypeEnum(Integer type, String destination, MqMessageProcessor processor, String desc) {
        this.type = type;
        this.destination = destination;
        this.desc = desc;
        this.processor = processor;
    }

    public Integer getType() {
        return type;
    }

    public String getDestination() {
        return destination;
    }

    public String getDesc() {
        return desc;
    }

    public void process(String content) {
        this.processor.process(content);
    }

    public static MqMessageTypeEnum valueOf(Integer type) {
        MqMessageTypeEnum[] values = MqMessageTypeEnum.values();
        for (MqMessageTypeEnum typeEnum : values) {
            if (typeEnum.getType().equals(type)) {
                return typeEnum;
            }
        }
        return null;
    }


    private interface MqMessageProcessor {
        void process(String content);
    }
}
